package user

import (
	"day12_12_homework/internal/api/request"
	"day12_12_homework/internal/models/service"
	"github.com/gin-gonic/gin"
	"net/http"
)

func UserRegister(c *gin.Context) {
	var json request.UserRegisterRequest
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	sms := service.HandleSendSms("register", json.Tel, json.Code)

	if sms != "" {
		c.JSON(http.StatusBadRequest, gin.H{
			"message": sms,
		})
		return
	}

	id, register := service.UserRegister(json.Tel, json.Password)

	if register != "" {
		c.JSON(http.StatusBadRequest, gin.H{
			"message": register,
		})
		return
	}

	service.CreateMessages(int(id))

	c.JSON(http.StatusOK, gin.H{
		"message": "注册成功",
		"data":    id,
	})

}
